<template>
    <el-card class="audit-user-card">
      <h2>用户审核</h2>
      <el-tabs v-model="activeTab">
        <el-tab-pane label="待审核学生" name="student">
          <el-table :data="students" stripe border v-loading="loading">
            <el-table-column prop="username" label="用户名" min-width="120">
              <template #default="{ row }">
                <el-link type="primary" @click="showDetail(row)">{{ row.username }}</el-link>
              </template>
            </el-table-column>
            <el-table-column prop="createTime" label="注册时间" min-width="160" />
            <el-table-column prop="status" label="状态" min-width="80">
              <template #default="{ row }">
                <el-tag>{{ statusMap[row.status] || row.status }}</el-tag>
              </template>
            </el-table-column>
            <el-table-column label="操作" min-width="160">
              <template #default="{ row }">
                <el-button type="success" size="small" @click="auditUser(row.id, 1)">通过</el-button>
                <el-button type="danger" size="small" @click="auditUser(row.id, 2)">拒绝</el-button>
              </template>
            </el-table-column>
          </el-table>
          <div class="pagination-bar">
            <el-pagination
              background
              layout="prev, pager, next, jumper"
              :total="studentTotal"
              :page-size="pageSize"
              :current-page="studentPage"
              @current-change="onStudentPageChange"
            />
          </div>
        </el-tab-pane>
        <el-tab-pane label="待审核企业" name="enterprise">  
          <el-table :data="enterprises" stripe border v-loading="loading">
            <el-table-column prop="username" label="用户名" min-width="120">
              <template #default="{ row }">
                <el-link type="primary" @click="showDetail(row)">{{ row.username }}</el-link>
              </template>
            </el-table-column>
            <el-table-column prop="createTime" label="注册时间" min-width="160" />
            <el-table-column prop="status" label="状态" min-width="80">
              <template #default="{ row }">
                <el-tag>{{ statusMap[row.status] || row.status }}</el-tag>
              </template>
            </el-table-column>
            <el-table-column label="操作" min-width="160">
              <template #default="{ row }">
                <el-button type="success" size="small" @click="auditUser(row.id, 1)">通过</el-button>
                <el-button type="danger" size="small" @click="auditUser(row.id, 2)">拒绝</el-button>
              </template>
            </el-table-column>
          </el-table>
          <div class="pagination-bar">
            <el-pagination
              background
              layout="prev, pager, next, jumper"
              :total="enterpriseTotal"
              :page-size="pageSize"
              :current-page="enterprisePage"
              @current-change="onEnterprisePageChange"
            />
          </div>
        </el-tab-pane>
      </el-tabs>
    </el-card>

  <el-dialog v-model="showDialog" title="详细信息" width="500px">
    <el-descriptions v-if="currentDetail" :column="1" border>
      <template v-if="activeTab === 'student'">
        <el-descriptions-item label="姓名">{{ currentDetail.name }}</el-descriptions-item>
        <el-descriptions-item label="性别">{{ currentDetail.gender === 1 ? '男' : '女' }}</el-descriptions-item>
        <el-descriptions-item label="出生日期">{{ currentDetail.birthDate }}</el-descriptions-item>
        <el-descriptions-item label="手机号">{{ currentDetail.phone }}</el-descriptions-item>
        <el-descriptions-item label="邮箱">{{ currentDetail.email }}</el-descriptions-item>
        <el-descriptions-item label="院校">{{ currentDetail.school }}</el-descriptions-item>
        <el-descriptions-item label="专业">{{ currentDetail.major }}</el-descriptions-item>
        <el-descriptions-item label="学历">{{ currentDetail.degree }}</el-descriptions-item>
        <el-descriptions-item label="毕业时间">{{ currentDetail.graduateDate }}</el-descriptions-item>
        <el-descriptions-item label="项目经历">{{ currentDetail.projectExperience }}</el-descriptions-item>
        <el-descriptions-item label="获奖情况">{{ currentDetail.award }}</el-descriptions-item>
        <el-descriptions-item label="证书">{{ currentDetail.certificate }}</el-descriptions-item>
      </template>
      <template v-else>
        <el-descriptions-item label="企业名称">{{ currentDetail.enterpriseName }}</el-descriptions-item>
        <el-descriptions-item label="所属行业">{{ currentDetail.industry }}</el-descriptions-item>
        <el-descriptions-item label="企业规模">{{ currentDetail.scale }}</el-descriptions-item>
        <el-descriptions-item label="企业地址">{{ currentDetail.address }}</el-descriptions-item>
        <el-descriptions-item label="联系方式">{{ currentDetail.contact }}</el-descriptions-item>
      </template>
    </el-descriptions>
  </el-dialog>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import {getPendingEnterprises, getPendingStudents, getStudentDetail, getEnterpriseDetail} from '@/api/admin'
import axios from '@/utils/axios'

const activeTab = ref('student')
const students = ref([])
const enterprises = ref([])
const allStudents = ref([])
const allEnterprises = ref([])
const studentTotal = ref(0)
const enterpriseTotal = ref(0)
const studentPage = ref(1)
const enterprisePage = ref(1)
const pageSize = 10
const loading = ref(false)
const showDialog = ref(false)
const currentDetail = ref(null)
const statusMap = { PENDING: '待审核', NORMAL: '正常', DISABLED: '禁用' }

const fetchStudents = async () => {
  loading.value = true
  try {
    const res = await getPendingStudents({})
    const data = res.data || res
    allStudents.value = data.list || []
    studentTotal.value = allStudents.value.length
    const start = (studentPage.value - 1) * pageSize
    const end = start + pageSize
    students.value = allStudents.value.slice(start, end)
  } catch (e) {}
  loading.value = false
}
const fetchEnterprises = async () => {
  loading.value = true
  try {
    const res = await getPendingEnterprises({})
    const data = res.data || res
    allEnterprises.value = data.list || []
    enterpriseTotal.value = allEnterprises.value.length
    const start = (enterprisePage.value - 1) * pageSize
    const end = start + pageSize
    enterprises.value = allEnterprises.value.slice(start, end)
  } catch (e) {}
  loading.value = false
}
const auditUser = async (id, status) => {
  try {
    await axios.put(`/admin/audit/${id}`, { userStatus: status })
    ElMessage.success(status === 1 ? '审核通过' : '已拒绝')
    if (activeTab.value === 'student') fetchStudents()
    else fetchEnterprises()
  } catch (e) {}
}
const showDetail = async (row) => {
  if (activeTab.value === 'student') {
    currentDetail.value = row.detail || await getStudentDetail(row.id)
    showDialog.value = true
  } else if (activeTab.value === 'enterprise') {
    currentDetail.value = row.detail || await getEnterpriseDetail(row.id)
    showDialog.value = true
  }
  showDialog.value = true
}
const onStudentPageChange = p => {
  studentPage.value = p
  const start = (studentPage.value - 1) * pageSize
  const end = start + pageSize
  students.value = allStudents.value.slice(start, end)
}
const onEnterprisePageChange = p => {
  enterprisePage.value = p
  const start = (enterprisePage.value - 1) * pageSize
  const end = start + pageSize
  enterprises.value = allEnterprises.value.slice(start, end)
}
onMounted(() => {
  fetchStudents()
  fetchEnterprises()
})
</script>

<style scoped>
.audit-user-card {
  max-width: 1100px;
  margin: 40px auto;
  padding: 30px 20px;
}
.pagination-bar {
  text-align: right;
  margin-top: 10px;
}
</style> 